//========================================================================
/**
 * Author   : cuisw <shaovie@gmail.com>
 * Date     : 2008-08-18 23:54
 */
//========================================================================

#ifndef _LOGTOFILES_H_
#define _LOGTOFILES_H_
#include "Pre.h"

#include "LogOutput.h" 
#include "LogManager.h"

/**
 * @class LogToFiles
 *
 * @brief 
 */
class LogToFiles : public LogOutput
{
    friend class LogManager;
public:
    LogToFiles ();

    virtual ~LogToFiles ();

    /**
     * Open the back end object. Perform any actions needed to prepare
     * the object for later logging operations.
     *
     * @retval 0 for success.
     * @retval -1 for failure.
     */
    virtual int open (const char *logger_arg,
			LogManager::Strategy *);

    // Close the output completely.
    virtual int close (void);

    //
    virtual void reset (void);

    // Update log strategy
    virtual void update_strategy (const char *, LogManager::Strategy *ls);

    /**
     * Process a log record.
     *
     * @retval -1 for failure; else it is customarily the number of bytes
     * processed, on zero is a no-op.
     */
    virtual int log (const char *log_msg, int len);
protected:
    //
    int create_file (void);

    //
    void build_filename (void);
    /**
     * @retval 0 file size is not overflow.
     * @retval 1 file size is overflow and rollover the files.
     */
    int check_file_size (void);

    //
    int rolloever_files (void);

    //
    void generate_new_file (void);

    //
    void calc_next_time (void);
private:
    NDK_HANDLE log_handle_;

    int      current_file_size_;

    //
    time_t   day_start_time_;

    //
    char *file_name_;

    char *log_dir_;

    //
    LogManager::Strategy *log_strategy_;

    //
    LogManager::Strategy *old_log_strategy_;

    //
    DateTime last_calc_dtime_;
};

#include "Post.h"
#endif

